{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solutions\n",
    "There will be many ways to solve these exercises which weren't covered in this chapter; however, the solutions below use only what has been introduced in chapter 2.\n",
    "\n",
    "## About the Data\n",
    "In this notebook, we will be working with Earthquake data from September 18, 2018 - October 13, 2018 (obtained from the US Geological Survey (USGS) using the [USGS API](https://earthquake.usgs.gov/fdsnws/event/1/))\n",
    "\n",
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('../../ch_02/data/parsed.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.9"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\n",
    "    (df.parsed_place.str.endswith('Japan')) & (df.magType == 'mb')\n",
    "].mag.quantile(0.95)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'23.13%'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"\"\"{df[df.parsed_place.str.endswith('Indonesia')].tsunami.value_counts(normalize=True).loc[1,]:.2%}\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cdi</th>\n",
       "      <th>dmin</th>\n",
       "      <th>felt</th>\n",
       "      <th>gap</th>\n",
       "      <th>mag</th>\n",
       "      <th>mmi</th>\n",
       "      <th>nst</th>\n",
       "      <th>rms</th>\n",
       "      <th>sig</th>\n",
       "      <th>time</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>tz</th>\n",
       "      <th>updated</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>15.000000</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>1.00</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>681.000000</td>\n",
       "      <td>6.810000e+02</td>\n",
       "      <td>681.0</td>\n",
       "      <td>681.0</td>\n",
       "      <td>6.810000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.440000</td>\n",
       "      <td>0.166199</td>\n",
       "      <td>2.400000</td>\n",
       "      <td>153.668120</td>\n",
       "      <td>0.500073</td>\n",
       "      <td>2.84</td>\n",
       "      <td>12.618209</td>\n",
       "      <td>0.151986</td>\n",
       "      <td>10.970631</td>\n",
       "      <td>1.538314e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.538402e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.501142</td>\n",
       "      <td>0.166228</td>\n",
       "      <td>4.626013</td>\n",
       "      <td>68.735302</td>\n",
       "      <td>0.696710</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.866963</td>\n",
       "      <td>0.084662</td>\n",
       "      <td>19.607150</td>\n",
       "      <td>5.965637e+08</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.010951e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.001000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>29.140000</td>\n",
       "      <td>-0.500000</td>\n",
       "      <td>2.84</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.000500</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.537247e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.537307e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.053000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>97.380000</td>\n",
       "      <td>-0.100000</td>\n",
       "      <td>2.84</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>0.106900</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.537854e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.537928e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.200000</td>\n",
       "      <td>0.112000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>149.140000</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>2.84</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>0.146300</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.538280e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.538428e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.900000</td>\n",
       "      <td>0.233000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>199.720000</td>\n",
       "      <td>0.900000</td>\n",
       "      <td>2.84</td>\n",
       "      <td>16.000000</td>\n",
       "      <td>0.187100</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>1.538821e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.538878e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>3.300000</td>\n",
       "      <td>1.414000</td>\n",
       "      <td>19.000000</td>\n",
       "      <td>355.910000</td>\n",
       "      <td>2.900000</td>\n",
       "      <td>2.84</td>\n",
       "      <td>61.000000</td>\n",
       "      <td>0.863400</td>\n",
       "      <td>129.000000</td>\n",
       "      <td>1.539461e+12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-480.0</td>\n",
       "      <td>1.539483e+12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             cdi        dmin       felt         gap         mag   mmi  \\\n",
       "count  15.000000  681.000000  15.000000  681.000000  681.000000  1.00   \n",
       "mean    2.440000    0.166199   2.400000  153.668120    0.500073  2.84   \n",
       "std     0.501142    0.166228   4.626013   68.735302    0.696710   NaN   \n",
       "min     2.000000    0.001000   1.000000   29.140000   -0.500000  2.84   \n",
       "25%     2.000000    0.053000   1.000000   97.380000   -0.100000  2.84   \n",
       "50%     2.200000    0.112000   1.000000  149.140000    0.400000  2.84   \n",
       "75%     2.900000    0.233000   1.000000  199.720000    0.900000  2.84   \n",
       "max     3.300000    1.414000  19.000000  355.910000    2.900000  2.84   \n",
       "\n",
       "              nst         rms         sig          time  tsunami     tz  \\\n",
       "count  681.000000  681.000000  681.000000  6.810000e+02    681.0  681.0   \n",
       "mean    12.618209    0.151986   10.970631  1.538314e+12      0.0 -480.0   \n",
       "std      9.866963    0.084662   19.607150  5.965637e+08      0.0    0.0   \n",
       "min      3.000000    0.000500    0.000000  1.537247e+12      0.0 -480.0   \n",
       "25%      6.000000    0.106900    0.000000  1.537854e+12      0.0 -480.0   \n",
       "50%     10.000000    0.146300    2.000000  1.538280e+12      0.0 -480.0   \n",
       "75%     16.000000    0.187100   12.000000  1.538821e+12      0.0 -480.0   \n",
       "max     61.000000    0.863400  129.000000  1.539461e+12      0.0 -480.0   \n",
       "\n",
       "            updated  \n",
       "count  6.810000e+02  \n",
       "mean   1.538402e+12  \n",
       "std    6.010951e+08  \n",
       "min    1.537307e+12  \n",
       "25%    1.537928e+12  \n",
       "50%    1.538428e+12  \n",
       "75%    1.538878e+12  \n",
       "max    1.539483e+12  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.parsed_place.str.endswith('Nevada')].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4\n",
    "Note we need to use `^Mexico` to get Mexico, but not New Mexico."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['ring_of_fire'] = df.parsed_place.str.contains(r'|'.join([\n",
    "    'Bolivia', 'Chile', 'Ecuador', 'Peru', 'Costa Rica', \n",
    "    'Guatemala', '^Mexico', 'Japan', 'Philippines',\n",
    "    'Indonesia', 'New Zealand', 'Antarctic', 'Canada',\n",
    "    'Fiji', 'Alaska', 'Washington', 'California', 'Russia', \n",
    "    'Taiwan', 'Tonga', 'Kermadec Islands'\n",
    "]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True     7188\n",
       "False    2144\n",
       "Name: ring_of_fire, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.ring_of_fire.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df.ring_of_fire, 'tsunami'].sum()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
